//
//  FCNetworkTool.m
//  FundChain
//
//  Created by 肖浩然的mac on 2018/3/7.
//  Copyright © 2018年 RUIXIN. All rights reserved.
//

#import "FCNetworkTool.h"

@implementation FCNetworkTool
+ (instancetype)sharedNetworkTool{
    static FCNetworkTool *instance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 实例化Manager
        NSURL *baseURL = [NSURL URLWithString:@"https://app.fund-chain.io/"];
        instance = [[FCNetworkTool alloc]initWithBaseURL:baseURL];
//        instance.requestSerializer = [[AFJSONRequestSerializer alloc]init];
        // 告知AFN让他给我返回原始的二进制数据(responseObject),我自己反序列化
        //        instance.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        // 让AFN支持,客户端向服务器发送特殊的二进制,比如 JSON plist
//        instance.requestSerializer = [AFJSONRequestSerializer serializer];
//        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"application/octet-stream", @"text/plain",@"multipart/form-data", nil];
    });
    
    return instance;
}

- (void)GETWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void(^)(id responseObject))success faile:(void(^)(NSError *error))faile{
    [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * task, NSDictionary * responseObject) {
        // AFN的回调,默认就是在主线程
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (faile) {
            faile(error);
        }
    }];
}

- (void)POSTWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void(^)(id responseObject))success faile:(void(^)(NSError *error))faile{
    
    [self POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * task, NSDictionary * responseObject) {
        // AFN的回调,默认就是在主线程
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (faile) {
            faile(error);
        }
    }];
}
@end
